package singleton;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ReflectTest {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        // 单例获取的对象地址信息
        System.out.println(HungerySingleton2.getInstance());
        // 破坏饿汉式
        // 获取构造
//        Constructor<HungerySingleton2> declaredConstructor = HungerySingleton2.class.getDeclaredConstructor();
//        // 不考虑 private 权限
//        declaredConstructor.setAccessible(true);
//        // 通过构造实例出对象
//        HungerySingleton2 hungerySingleton2 = declaredConstructor.newInstance();
//        System.out.println(hungerySingleton2);


    }
}
class HungerySingleton2{
    private static HungerySingleton2 instance = new HungerySingleton2();

    // 构造私有
    private HungerySingleton2(){
        if(instance != null){
            throw new RuntimeException("单例对象不能创建多实例！");
        }
    }

    // 提供对外获取方式
    public static HungerySingleton2 getInstance() {
        return instance;
    }
}